/*
 * Created on 2009-9-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sitech.portal.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.sitech.ibnms.common.form.TreeNode;
import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.ibnms.util.AppException;
import com.sitech.portal.system.form.SubSystemForm;
import com.sitech.portal.system.service.ISysModuleService;
import com.sitech.portal.system.service.ISysSubSystemService;

/**
 * @author niezl
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class SubSystemAction extends JGridBaseAction {

	private SubSystemForm theForm;
	private ISysSubSystemService sysSubSystemService;
	private ISysModuleService sysModuleService;
	private String flag ;
	private List<TreeNode> subSystemList = new ArrayList<TreeNode>();
	
	
	public ISysModuleService getSysModuleService() {
		return sysModuleService;
	}

	public void setSysModuleService(ISysModuleService sysModuleService) {
		this.sysModuleService = sysModuleService;
	}

	public List<TreeNode> getSubSystemList() {
		return subSystemList;
	}

	public void setSubSystemList(List<TreeNode> subSystemList) {
		this.subSystemList = subSystemList;
	}

	public SubSystemForm getTheForm() {
		return theForm;
	}

	public void setTheForm(SubSystemForm theForm) {
		this.theForm = theForm;
	}

	public ISysSubSystemService getSysSubSystemService() {
		return sysSubSystemService;
	}

	public void setSysSubSystemService(ISysSubSystemService sysSubSystemService) {
		this.sysSubSystemService = sysSubSystemService;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String loadListSubSystem() {
		IPage result = sysSubSystemService.findListSubSystem(this.getPageForm());
		this.setResult(result);
		return SUCCESS;
	}

	public String editSubSystem() {
		String[] SYSTEM_IDS = theForm.getSYSTEM_IDS();
		if (theForm.getISMP_JSP_COMMAND() != null
				&& theForm.getISMP_JSP_COMMAND().equals("add")) {
			theForm.setSYSTEM_ID(null);
			return SUCCESS;

		} else if (theForm.getISMP_JSP_COMMAND() != null
				&& theForm.getISMP_JSP_COMMAND().equals("modify")) {
			HashMap map = sysSubSystemService
					.findObjForTB_SYS_SUB_SYSTEMByPK(SYSTEM_IDS[0]);
			theForm.setSYSTEM_NAME((String) map.get("SYSTEM_NAME"));
			theForm.setSYSTEM_PATH((String) map.get("SYSTEM_PATH"));
			theForm.setVIEW_ORDER((String) map.get("VIEW_ORDER"));
			theForm.setSYSTEM_LAYOUT((String) map.get("SYSTEM_LAYOUT"));
			theForm.setENABLE((String) map.get("ENABLE"));
			theForm.setSYSTEM_ID(SYSTEM_IDS[0]);
		}

		return SUCCESS;
	}

	public String saveSubSystem() {
		flag = "true";
		try{
			HashMap map = new HashMap();
			map.put("SYSTEM_ID", theForm.getSYSTEM_ID());
			map.put("SYSTEM_NAME", theForm.getSYSTEM_NAME());
			map.put("SYSTEM_PATH", theForm.getSYSTEM_PATH());
			map.put("VIEW_ORDER", theForm.getVIEW_ORDER());
			map.put("SYSTEM_LAYOUT", theForm.getSYSTEM_LAYOUT());
			map.put("ENABLE", theForm.getENABLE());
			if (theForm.getISMP_JSP_COMMAND() != null
					&& theForm.getISMP_JSP_COMMAND().equals("add")) {
				sysSubSystemService.add(map);
			} else if (theForm.getISMP_JSP_COMMAND() != null
					&& theForm.getISMP_JSP_COMMAND().equals("modify")) {
				sysSubSystemService.update(map);
			}
		}catch(Exception e){
			flag = e.getMessage();
		}

		return SUCCESS;
	}

	public String delSubSystem() {
		flag = "true";
		String[] SYSTEM_IDS = theForm.getSYSTEM_IDS();
		try {
			for (int i = 0; i < SYSTEM_IDS.length; i++) {
				sysSubSystemService
						.deleteForTB_SYS_SUB_SYSTEMByPK(SYSTEM_IDS[i]);
			}
		} catch (Exception e) {
			flag = e.getMessage();
		}

		return SUCCESS;
	}
}